/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.video.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.util.ListBeanUtils;
import com.hopes.bizz.poll.video.entity.PollVdVideoEntity;
import com.hopes.bizz.poll.video.entity.PollVdVideoRegionEntity;
import com.hopes.bizz.poll.video.mapper.PollVdVideoMapper;
import com.hopes.bizz.poll.video.mapper.PollVdVideoRegionMapper;
import com.hopes.bizz.poll.video.service.PollVdVideoRegionService;
import com.hopes.bizz.poll.video.vo.VdVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频区域管理
 *
 * @author pig
 * @date 2023-09-19 15:32:37
 */
@Service
public class PollVdVideoRegionServiceImpl extends ServiceImpl<PollVdVideoRegionMapper, PollVdVideoRegionEntity> implements PollVdVideoRegionService {

    @Autowired
    private PollVdVideoRegionMapper pollVdVideoRegionMapper;
    @Autowired
    private PollVdVideoMapper pollVdVideoMapper;
    @Override
    public JSONArray listTree(PollVdVideoRegionEntity videoRegion, boolean showVideo) {
        return this.listTreeNode(videoRegion, showVideo);
    }

    private JSONArray listTreeNode(PollVdVideoRegionEntity vdVideoRegion, Boolean showVideo) {
        JSONArray nodes = new JSONArray();
        PollVdVideoRegionEntity videoRegion = new PollVdVideoRegionEntity();
        videoRegion.setParentId(vdVideoRegion.getId());
        QueryWrapper<PollVdVideoRegionEntity> regionQueryWrapper = new QueryWrapper<>();
        regionQueryWrapper.setEntity(videoRegion);
        regionQueryWrapper.orderByDesc("sort_num");
        List<PollVdVideoRegionEntity> videoRegionList = pollVdVideoRegionMapper.selectList(regionQueryWrapper);
        for (int i = 0; i < videoRegionList.size(); i++) {
            PollVdVideoRegionEntity entity = videoRegionList.get(i);
            JSONObject node = new JSONObject();
            node.put("label", entity.getRegionName());
            node.put("id", entity.getId());
            node.put("value", entity.getId());
            node.put("sortNum", entity.getSortNum());
            node.put("type", "region");
            JSONArray children = this.listTreeNode(entity, showVideo);

            if (children.size() > 0) {
                node.put("children", children);
            }
            nodes.add(node);
        }

        if(showVideo){
            // 查询区域下面的视频
            PollVdVideoEntity pollVdVideoEntity = new PollVdVideoEntity();
            pollVdVideoEntity.setRegionId(vdVideoRegion.getId());
            pollVdVideoEntity.setIsUse(true);
            QueryWrapper<PollVdVideoEntity> videoQueryWrapper = new QueryWrapper<>();
            videoQueryWrapper.setEntity(pollVdVideoEntity);
            List<PollVdVideoEntity> videoEntityList = pollVdVideoMapper.selectList(videoQueryWrapper);
            List<VdVideo> videoList = ListBeanUtils.copyListProperties(videoEntityList, VdVideo::new);
            for(int j = 0; j < videoList.size(); j++){
                VdVideo vdVideo = videoList.get(j);
                JSONObject videoNode = this.buildVideoObj(vdVideo);
                nodes.add(videoNode);
            }
        }
        return nodes;
    }

    private JSONObject buildVideoObj(VdVideo video){
        JSONObject node = new JSONObject();
        node.put("label", video.getVideoName());
        node.put("id", video.getId());
        node.put("value", video.getId());
        node.put("type", "video");
        node.put("videoCode", video.getVideoCode());
        node.put("playMode",video.getPlayMode());
        return node;
    }

}